<?php

/**
 * This is the model class for table "seliste".
 *
 * The followings are the available columns in table 'seliste':
 * @property integer $id
 * @property string $nom
 * @property string $prenom
 * @property string $pseudo
 * @property string $adr
 * @property string $cp
 * @property string $ville
 * @property string $telfixe
 * @property string $telport
 * @property string $email
 * @property string $pwd
 * @property string $modecontact
 * @property integer $v_adr
 * @property integer $v_cp
 * @property integer $v_ville
 * @property integer $v_telfixe
 * @property integer $v_email
 * @property integer $v_telport
 * @property integer $v_nom
 * @property integer $v_prenom
 * @property integer $v_pseudo
 * @property integer $solde
 * @property string $date_inscr
 * @property integer $admin
 * @property string $theme
 * @property integer $hum
 *
 * The followings are the available model relations:
 * @property Demande[] $demandes
 * @property Demande[] $demandes1
 * @property Groupe[] $groupes
 * @property Offre[] $offres
 * @property Grain[] $grains
 */
class Seliste extends CActiveRecord
{

	public $groupe;
	public $pwd2;
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'seliste';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id, nom, prenom, pseudo, adr, cp, ville', 'required'),
			array('pwd, pwd2', 'required', 'on'=>'insert, update_pass'),
			array('pwd, pwd2', 'length', 'min'=>6, 'max'=>40),
			array('pwd', 'compare', 'compareAttribute'=>'pwd2', 'on'=>'insert, update_pass'),
			array('id, v_adr, v_cp, v_ville, v_telfixe, v_email, v_telport, v_nom, v_prenom, v_pseudo, solde, admin, hum, notif_demande, notif_message', 'numerical', 'integerOnly'=>true),
			array('nom, prenom, pseudo, adr, email', 'length', 'max'=>255),
			array('cp', 'length', 'max'=>10),
			array('ville, telfixe, telport', 'length', 'max'=>45),
			array('modecontact, hum, notif_demande, notif_message', 'length', 'max'=>1),
			array('modecontact','checkmode'),
			array('email','email'),
			array('date_inscr', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, nom, prenom, pseudo, adr, cp, ville, telfixe, telport, email, modecontact, v_adr, v_cp, v_ville, v_telfixe, v_email, v_telport, v_nom, v_prenom, v_pseudo, solde, date_inscr, admin, hum', 'safe', 'on'=>'search'),
		);
	}
	
	public function checkmode($attribute,$params)
	{
		switch ($this->modecontact) {
			case 1	:	/* email */
						if ($this->email == "")  $this->addError('email','L\'email doit être saisi.');
						if ($this->v_email <> 1) $this->addError('email','L\'email doit être visible.');						
						break;
			case 2	: 	/* Téléphone fixe */
						if ($this->telfixe == "")  $this->addError('telfixe','Le numéro de téléphone fixe doit être saisi.');
						if ($this->v_telfixe <> 1) $this->addError('telfixe','Le numéro de téléphone fixe doit être visible.');
						break;
			case 3 	: 	/* Téléphone portable */
						if ($this->telport == "")  $this->addError('telport','Le numéro de téléphone portable doit être saisi.');
						if ($this->v_telport <> 1) $this->addError('telport','Le numéro de téléphone portable doit être visible.');
						break;
		}
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'demandes' => array(self::HAS_MANY, 'Demande', 'idseliste_saisie'),
			'demandes1' => array(self::HAS_MANY, 'Demande', 'idseliste'),
			'groupes' => array(self::MANY_MANY, 'Groupe', 'membre(idseliste, idgroupe)'),
			'offres' => array(self::HAS_MANY, 'Offre', 'idseliste'),
			'grains' => array(self::MANY_MANY, 'Grain', 'propose(idseliste, idgrain)'),
			'messagesRecus' => array(self::HAS_MANY, 'Destinataire', 'idseliste'),
			'messagesEmis' => array(self::HAS_MANY, 'Message', 'idseliste_em'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Numéro',
			'nom' => 'Nom',
			'prenom' => 'Prénom',
			'pseudo' => 'Pseudo',
			'adr' => 'Adresse',
			'cp' => 'Code postal',
			'ville' => 'Ville',
			'telfixe' => 'Téléphone fixe',
			'telport' => 'Téléphone portable',
			'email' => 'Email',
			'pwd' => 'Mot de passe',
			'pwd2' => 'Confirmation',
			'modecontact' => 'Mode de contact préféré',
			'v_adr' => 'Adresse',
			'v_cp' => 'Code postal',
			'v_ville' => 'Ville',
			'v_telfixe' => 'Téléphone fixe',
			'v_email' => 'Email',
			'v_telport' => 'Téléphone portable',
			'v_nom' => 'Nom',
			'v_prenom' => 'Prénom',
			'v_pseudo' => 'Pseudo',
			'solde' => 'Solde de tuyaux',
			'date_inscr' => 'Date d\'inscription',
			'admin' => 'Administrateur',
			'groupe' => 'Groupe',
			'notif_demande' => 'une demande',
			'notif_message' => 'un message',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('t.id',$this->id);
		$criteria->compare('nom',$this->nom,true);
		$criteria->compare('prenom',$this->prenom,true);
		$criteria->compare('pseudo',$this->pseudo,true);
		$criteria->compare('adr',$this->adr,true);
		$criteria->compare('cp',$this->cp,true);
		$criteria->compare('ville',$this->ville,true);
		if ($this->nom <> "") $criteria->compare('v_nom',1);
		if ($this->prenom <> "") $criteria->compare('v_prenom',1);
		if ($this->pseudo <> "") $criteria->compare('v_pseudo',1);
		if ($this->adr <> "") $criteria->compare('v_adr',1);
		if ($this->cp <> "") $criteria->compare('v_cp',1);
		if ($this->ville <> "") $criteria->compare('v_ville',1);
		/*$criteria->compare('telfixe',$this->telfixe,true);
		$criteria->compare('telport',$this->telport,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('pwd',$this->pwd,true);
		$criteria->compare('modecontact',$this->modecontact,true);
		$criteria->compare('v_telfixe',$this->v_telfixe);
		$criteria->compare('v_email',$this->v_email);
		$criteria->compare('v_telport',$this->v_telport);
		$criteria->compare('solde',$this->solde);
		$criteria->compare('date_inscr',$this->date_inscr,true);
		$criteria->compare('admin',$this->admin);
		$criteria->compare('admin_seliste',$this->admin_seliste);
		$criteria->compare('admin_grain',$this->admin_grain);*/
		if ($this->groupe>0) {
			$criteria->with = array('groupes');
			$criteria->compare('groupes.id',$this->groupe);
			$criteria->together = true;
		}
		
		$criteria->compare('hum',1);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array("defaultOrder"=>"nom asc"),
			'pagination' => array(
								'pageSize' => 40,
							),  
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Seliste the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function getGroupes() {
		$groupes=CHtml::listData($this->groupes,'id','lib');
		$retour = "";
		foreach($groupes as $key=>$value) {
			$retour .= "<span class=\"label label-info\">" . $value . "</span> ";
		}
		return $retour;
	}
	
	public function getGrains() {
		$tgrains = $this->grains;
		$grains=CHtml::listData($tgrains,'id',function($tgrains) {
			if ($tgrains->status==1 || $tgrains->idseliste==Yii::app()->user->id || Yii::app()->user->admin) 
				return "<span class=\"label label-info\">" . $tgrains->idtype0->lib . " / " . $tgrains->iddomaine0->lib . " / " . $tgrains->nom . "</span>";
		});
		$retour = "";
		foreach($grains as $key=>$value) {
			$retour .= $value . " ";
		}
		return $retour;
	}
	
	public function getSelistesGrain ($id) {
		$selistes = Seliste::model()->findAll(array("with"=>"grains","condition"=>"grains.id = " . $id . " and grains.status=1"));
		return $selistes;
	}
	
	// nombre de messages à lire
	public function getNbUnread() {
		return Destinataire::model()->countByAttributes(
				array(
			            'idseliste'=> Yii::app()->user->id,
						'lu'=>NULL,
						'del_rec'=>NULL
				)
        );
	}
	
	// nombre de messages envoyés non lus par les destinataires
	public function getNbSentUnread() {
		$criteria=new CDbCriteria;
		$criteria->compare('lu',NULL);
		$criteria->compare('del_rec',NULL);
		$criteria->compare('idseliste_em',Yii::app()->user->id);
		$criteria->compare('auto',0);
		$criteria->with = array('idmessage0');
		$criteria->group = 't.idmessage';
		return Destinataire::model()->count($criteria);
	}
}
